package com.winning.zjjh.quarzt;

import com.winning.zjjh.quarzt.job.*;
import org.apache.commons.lang.StringUtils;
import org.quartz.*;
import org.quartz.impl.triggers.CronTriggerImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.text.ParseException;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

/**
 * @Author: xkh@winning.com.cn
 * @Desc: TODO
 * @Date: 2019-03-21 14:39
 **/
@Configuration
@ConfigurationProperties(prefix = "cron.quzrzt",ignoreInvalidFields = true,ignoreUnknownFields = true)
public class ConfigureQuarzt {

    private JobDataMap jobDataMap = new JobDataMap();

    private Map cronCollection = new HashMap();

    @Autowired
    ConfigureQuarzt(ApplicationContext applicationContext) {
        jobDataMap.put("applicationContext", applicationContext);
    }

    @Bean
    JobDetail chfsJobDetail() {
        return createJobDetail(ChfsJob.class,jobDataMap,"chfsjob","group0");
    }

    @Bean
    JobDetail ch42JobDetail() {
        return createJobDetail(Ch42Job.class,jobDataMap,"ch42job","group0");
    }

    @Bean
    JobDetail ycjaJobDetail() {
        return createJobDetail(YcjaJob.class,jobDataMap,"ycjajob","group0");
    }

    @Bean
    JobDetail ebgcJobDetail() {
        return createJobDetail(EbgcJob.class,jobDataMap,"ebgcjob","group0");
    }

    @Bean
    JobDetail ebjaJobDetail() {
        return createJobDetail(EbjaJob.class,jobDataMap,"ebjajob","group0");
    }

    @Bean
    JobDetail gesfJobDetail() {
        return createJobDetail(GesfJob.class,jobDataMap,"gesfjob","group0");
    }

    @Bean
    JobDetail geyyJobDetail() {
        return createJobDetail(GeyyJob.class,jobDataMap,"geyyjob","group0");
    }

    @Bean
    JobDetail jktjJobDetail() {
        return createJobDetail(JktjJob.class,jobDataMap,"jktjjob","group0");
    }



    @Bean
    JobDetail regcardJobDetail() {
        return JobBuilder.newJob(RegCardJob.class)
                .withIdentity("regcardjob", "group0")
                .usingJobData(jobDataMap)
                .storeDurably(true)
                .build();
    }

    @Bean
    JobDetail childbirthJobDetail() {
        return JobBuilder.newJob(ChildbirthJob.class)
                .withIdentity("childbirthjob", "group0")
                .usingJobData(jobDataMap)
                .storeDurably(true)
                .build();
    }

    @Bean
    JobDetail firstvisitJobDetail() {
        return JobBuilder.newJob(FirstVisitJob.class)
                .withIdentity("firstvisitjob", "group0")
                .usingJobData(jobDataMap)
                .storeDurably(true)
                .build();
    }

    @Bean
    JobDetail furthervisitJobDetail() {
        return JobBuilder.newJob(FurtherVisitJob.class)
                .withIdentity("furthervisitjob", "group0")
                .usingJobData(jobDataMap)
                .storeDurably(true)
                .build();
    }

    @Bean
    JobDetail pregnancyJobDetail() {
        return JobBuilder.newJob(PregnancyHistoryJob.class)
                .withIdentity("pregnancyhistoryjob", "group0")
                .usingJobData(jobDataMap)
                .storeDurably(true)
                .build();
    }

    @Bean
    JobDetail ebdjjkJobDetail() {
        return JobBuilder.newJob(EbdjjkJob.class)
                .withIdentity("ebdjjkjob", "group0")
                .usingJobData(jobDataMap)
                .storeDurably(true)
                .build();
    }

    @Bean
    JobDetail xsefsJobDetail() {
        return JobBuilder.newJob(XsefsJob.class)
                .withIdentity("xsefsjob", "group0")
                .usingJobData(jobDataMap)
                .storeDurably(true)
                .build();
    }

    @Bean
    Trigger chfsTrg() throws ParseException {
        return createTrigger("chfsjob","group0","chfstrg","group0",String.valueOf(cronCollection.get("chfs")));
    }

    @Bean
    Trigger ch42Trg() throws ParseException {
        return createTrigger("ch42job","group0","ch42trg","group0",String.valueOf(cronCollection.get("ch42")));
    }

    @Bean
    Trigger ycjaTrg() throws ParseException {
        return createTrigger("ycjajob","group0","ycjatrg","group0",String.valueOf(cronCollection.get("ycja")));
    }

    @Bean
    Trigger ebgcTrg() throws ParseException {
        return createTrigger("ebgcjob","group0","ebgctrg","group0",String.valueOf(cronCollection.get("ebgc")));
    }

    @Bean
    Trigger ebjaTrg() throws ParseException {
        return createTrigger("ebjajob","group0","ebjatrg","group0",String.valueOf(cronCollection.get("ebja")));
    }

    @Bean
    Trigger gesfTrg() throws ParseException {
        return createTrigger("gesfjob","group0","gesftrg","group0",String.valueOf(cronCollection.get("gesf")));
    }

    @Bean
    Trigger geyyTrg() throws ParseException {
        return createTrigger("geyyjob","group0","geyytrg","group0",String.valueOf(cronCollection.get("geyy")));
    }

    @Bean
    Trigger jktjTrg() throws ParseException {
        return createTrigger("jktjjob","group0","jktjtrg","group0",String.valueOf(cronCollection.get("jktj")));
    }


    @Bean
    Trigger regcardTrg() throws ParseException {
        CronTriggerImpl trigger = new CronTriggerImpl();
        trigger.setJobName("regcardjob");
        trigger.setJobGroup("group0");
        trigger.setName("regcardtrg");
        trigger.setGroup("group0");
        trigger.setCronExpression(String.valueOf(cronCollection.get("regcard")));
        return trigger;
    }

    @Bean
    Trigger childbirthTrg() throws ParseException {
        CronTriggerImpl trigger = new CronTriggerImpl();
        trigger.setJobName("childbirthjob");
        trigger.setJobGroup("group0");
        trigger.setName("childbirthtrg");
        trigger.setGroup("group0");
        trigger.setCronExpression(String.valueOf(cronCollection.get("childbirth")));
        return trigger;
    }

    @Bean
    Trigger firstvisitTrg() throws ParseException {
        CronTriggerImpl trigger = new CronTriggerImpl();
        trigger.setJobName("firstvisitjob");
        trigger.setJobGroup("group0");
        trigger.setName("firstvisittrg");
        trigger.setGroup("group0");
        trigger.setCronExpression(String.valueOf(cronCollection.get("firstvisit")));
        return trigger;
    }

    @Bean
    Trigger furthervisitTrg() throws ParseException {
        CronTriggerImpl trigger = new CronTriggerImpl();
        trigger.setJobName("furthervisitjob");
        trigger.setJobGroup("group0");
        trigger.setName("furthervisitTrg");
        trigger.setGroup("group0");
        trigger.setCronExpression(String.valueOf(cronCollection.get("furthervisit")));
        return trigger;
    }

    @Bean
    Trigger PregnancyHistoryTrg() throws ParseException {
        CronTriggerImpl trigger = new CronTriggerImpl();
        trigger.setJobName("pregnancyhistoryjob");
        trigger.setJobGroup("group0");
        trigger.setName("pregnancyhistorytrg");
        trigger.setGroup("group0");
        trigger.setCronExpression(String.valueOf(cronCollection.get("pregnancy")));
        return trigger;
    }

    @Bean
    Trigger EbdjjkTrg() throws ParseException {
        CronTriggerImpl trigger = new CronTriggerImpl();
        trigger.setJobName("ebdjjkjob");
        trigger.setJobGroup("group0");
        trigger.setName("ebdjjk");
        trigger.setGroup("group0");
        trigger.setCronExpression(String.valueOf(cronCollection.get("ebdjjk")));
        return trigger;
    }

    @Bean
    Trigger XsefsTrg() throws ParseException {
        CronTriggerImpl trigger = new CronTriggerImpl();
        trigger.setJobName("xsefsjob");
        trigger.setJobGroup("group0");
        trigger.setName("xsrfs");
        trigger.setGroup("group0");
        trigger.setCronExpression(String.valueOf(cronCollection.get("xsefs")));
        return trigger;
    }

    /**
     *
     * @param job         作业的Job的Class对象
     * @param jobDataMap  job需要的数据集
     * @param jobname     job名称
     * @param jobgroup    job所属组
     * @return 新的JobDetail实例
     */
    private JobDetail createJobDetail(Class<? extends Job> job,JobDataMap jobDataMap,String jobname,String jobgroup) {

        if (Objects.isNull(job) || StringUtils.isEmpty(jobname)) {
            throw new IllegalArgumentException("[job、jobname] arguments must not be empty");
        }

        JobBuilder jobBuilder = JobBuilder.newJob(job);

        if (StringUtils.isNotEmpty(jobgroup)) {
            jobBuilder.withIdentity(jobname,jobgroup);
        }else {
            jobBuilder.withIdentity(jobname);
        }

        if (Objects.nonNull(jobDataMap)) {
            jobBuilder.usingJobData(jobDataMap);
        }

        return jobBuilder.storeDurably(true).build();
    }


    /**
     *
     * @param jobname   job名称
     * @param jobgroup  job所属组
     * @param trgname   触发器名称
     * @param trggroup  触发器所属组
     * @param cron      cron定时时间模式
     * @return 新的触发器对象
     */
    private Trigger createTrigger(String jobname, String jobgroup, String trgname, String trggroup, String cron) throws ParseException {

        if (StringUtils.isEmpty(jobname)
                || StringUtils.isEmpty(trgname)
                || StringUtils.isEmpty(cron)) {
            throw new IllegalArgumentException("[jobname、trgname、cron] arguments must not be empty");
        }

        CronTriggerImpl trigger = new CronTriggerImpl();
        trigger.setJobName(jobname);
        trigger.setName(trgname);
        trigger.setCronExpression(cron);

        if (StringUtils.isNotEmpty(jobgroup)) {
            trigger.setJobGroup(jobgroup);
        }

        if (StringUtils.isNotEmpty(trggroup)) {
            trigger.setGroup(trggroup);
        }

        return trigger;
    }


    public Map getCronCollection() {
        return cronCollection;
    }
}
